﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PlayingCards.Model.Entity;
using PlayingCards.Model.Result;
using PlayingCards.Service.IEntityService;

namespace PlayingCards.Api.Controllers
{
    /// <summary>
    ///  房间接口
    /// </summary>
    [AllowAnonymous]
    public class RoomController : BaseController
    {
        private readonly ILogger _logger;
        private readonly IRoomService _roomService;
        public RoomController(ILogger<RoomController> logger, IRoomService roomService)
        {
            _logger = logger;
            _roomService = roomService;
        }


        /// <summary>
        ///  创建房间
        /// </summary>
        /// <param name="roomcode">房间码</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<string>> CreateRoom(string roomcode)
        {
            try
            {
                return await _roomService.CreateRoom(roomcode);
            }
            catch (Exception ex)
            {
                _logger.LogError($"创建房间异常{ex}");
                return new Result<string> { Status = false };
            }
        }

        /// <summary>
        ///  查询房间
        /// </summary>
        /// <param name="RoomNumber"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<string>> GetRoom(string RoomNumber)
        {
            try
            {
                return await _roomService.GetRoom(RoomNumber);
            }
            catch (Exception ex)
            {
                _logger.LogError($"查询房间异常{ex}");
                return new Result<string> { Status = false };
            }
        }

        /// <summary>
        ///  进入房间（房间详情）
        /// </summary>
        /// <param name="roomcode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<Room>> JoinRoom(string roomcode)
        {
            try
            {
                return await _roomService.JoinRoom(roomcode);
            }
            catch (Exception ex)
            {
                _logger.LogError($"进入房间异常{ex}");
               return new Result<Room> { Status = false };
            }
        }
    }
}
